<?php

namespace app\admin\service;

use app\admin\model\VideoCategory;
use app\admin\model\videolevel;
use app\admin\model\videoList;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class VideoService
{
    public static function goAdd()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'level_id' => 'require',
                'video_title' => 'require',
                'video_thumb' => 'require',
                'video_url' => 'require',
            ];
            $message = [
                'level_id.require' => "视频等级不能为空",
                'video_title.require' => "视频标题不能为空",
                'video_thumb.require' => "视频缩略图不能为空",
                'video_url.require' => "视频内容不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                videoList::create($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'video_id' => 'require',
                'level_id' => 'require',
                'video_title' => 'require',
                'video_thumb' => 'require',
                'video_url' => 'require',
            ];
            $message = [
                'video_id.require' => "视频ID不能为空",
                'level_id.require' => "视频等级不能为空",
                'video_title.require' => "视频标题不能为空",
                'video_thumb.require' => "视频缩略图不能为空",
                'video_url.require' => "视频内容不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                videoList::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRecommend()
    {
        $data = Request::post();
        $model = videoList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['is_recommend' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goStatus()
    {
        $data = Request::post();
        $model = videoList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['video_status' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemove($id)
    {
        $model = videoList::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goAddLevel()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'level_title' => 'require',
            ];
            $message = [
                'level_title.require' => "等级名称不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $exist = VideoLevel::checkExist(0, $data['level_title']);
                if ($exist) return ['msg' => '等级名称已存在', 'code' => 201];
                VideoLevel::create($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEditLevel()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'level_id' => 'require',
                'level_title' => 'require',
            ];
            $message = [
                'level_id.require' => "等级ID不能为空",
                'level_title.require' => "等级名称不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['imageFile']);
            try {
                $exist = VideoLevel::checkExist($data['level_id'], $data['level_title']);
                if ($exist) return ['msg' => '等级名称已存在', 'code' => 201];
                VideoLevel::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatusLevel()
    {
        $data = Request::post();
        $model = VideoLevel::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['level_status' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemoveLevel($id)
    {
        $model = VideoLevel::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goAddCategory()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'category_title' => 'require',
            ];
            $message = [
                'category_title.require' => "分类名称不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $exist = VideoCategory::checkExist(0, $data['category_title']);
                if ($exist) return ['msg' => '分类名称已存在', 'code' => 201];
                VideoCategory::create($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEditCategory()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'category_id' => 'require',
                'category_title' => 'require',
            ];
            $message = [
                'category_id.require' => "分类ID不能为空",
                'category_title.require' => "分类名称不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['imageFile']);
            try {
                $exist = VideoCategory::checkExist($data['category_id'], $data['category_title']);
                if ($exist) return ['msg' => '分类名称已存在', 'code' => 201];
                VideoCategory::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatusCategory()
    {
        $data = Request::post();
        $model = VideoCategory::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['category_status' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemoveCategory($id)
    {
        $model = VideoCategory::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}